package com.smartx.app.util.libimage;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.GlideModule;

import java.io.File;

/**
 * @author: tangjie
 * @date: 2017/4/20 10:08
 * @Description:
 */

public class COGlideModule implements GlideModule {

    @Override

    public void applyOptions(Context context, GlideBuilder builder) {

        // Apply options to the builder here.
        int maxMemory = (int) Runtime.getRuntime().maxMemory();//获取系统分配给应用的总内存大小

        int memoryCacheSize = maxMemory / 15;//设置图片内存缓存占用//设置内存缓存大小

        builder.setBitmapPool(new LruBitmapPool(memoryCacheSize));//设置BitmapPool缓存内存大小
        if (Build.VERSION.SDK_INT >= 18) {
            builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        }
        builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
        final File cacheDir = new File(Environment.getExternalStorageDirectory() + "/carowner/images/");
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
        builder.setDiskCache(new DiskCache.Factory() {
            @Override
            public DiskCache build() {
                return DiskLruCacheWrapper.get(cacheDir, 150 * 1024 * 1024);
            }
        });

    }

    @Override

    public void registerComponents(Context context, Glide glide) {

        // register ModelLoaders here.

    }

}